package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import bean.*;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import service.SyncService;


@Controller("SyncManage")
@Scope("prototype")
public class SyncManageAction {
	@Resource SyncService syncService;
/*
	private Student student;
	private Course course;
	private SingleCourse singleCourse;
	private CourseBehave courseBehave;
	private Course_Student course_Student;*/
	private Map<String, Object> syncJson;		//回传的数据
	private String studentArray;
	private String courseArray;
	private String singleCourseArray;
	private String courseBehaveArray;
	private String course_StudentArray;
	private int tid;
	
	
	public int getTid() {
		return tid;
	}

	public void setTid(int tid) {
		this.tid = tid;
	}

	public Map<String, Object> getSyncJson() {
		return syncJson;
	}

	public void setSyncJson(Map<String, Object> syncJson) {
		this.syncJson = syncJson;
	}
	
	public String getStudentArray() {
		return studentArray;
	}

	public void setStudentArray(String studentArray) {
		this.studentArray = studentArray;
	}

	public String getCourseArray() {
		return courseArray;
	}

	public void setCourseArray(String courseArray) {
		this.courseArray = courseArray;
	}

	public String getSingleCourseArray() {
		return singleCourseArray;
	}

	public void setSingleCourseArray(String singleCourseArray) {
		this.singleCourseArray = singleCourseArray;
	}

	public String getCourseBehaveArray() {
		return courseBehaveArray;
	}

	public void setCourseBehaveArray(String courseBehaveArray) {
		this.courseBehaveArray = courseBehaveArray;
	}

	public String getCourse_StudentArray() {
		return course_StudentArray;
	}

	public void setCourse_StudentArray(String course_StudentArray) {
		this.course_StudentArray = course_StudentArray;
	}

//====================================Action=========================================
	
	/**
	 * 恢复数据
	 * 根据获取的teacher_id来获取服务器端的所有对应数据，存入map后传到安卓端
	 * @return recoverfinish
	 */
	public String recover()
	{
		System.out.println(getTid());
		Map<String, Object> map = syncService.recoverData(getTid());
		if(map.isEmpty())								//云端没有可恢复的数据
		{
			System.out.println("云端无可恢复数据");
			map.put("result", "nodata");					
			this.setSyncJson(map);
			return "recoverfinish";
		}
		if(map.size() < 5)
		{
			System.out.println("云端数据不完整");
			map.put("result", "lackdata");					
			this.setSyncJson(map);
			return "recoverfinish";
		}
		map.put("result", "success");
		this.setSyncJson(map);
		syncService.updateSyncTime(getTid(), new Date());
		System.out.println("数据获取成功");
		return "recoverfinish";
	}
	
	/**
	 * 备份数据
	 * 获取安卓端传来的所有数据，覆盖/存入服务器端
	 * @return backupfinish
	 */
	@SuppressWarnings({ "static-access", "unchecked" })
	public String backup()
	{
		Map<String, Object> map = new HashMap<String, Object>();
		List<Student> studentlist = new ArrayList<>();
		List<Course> courselist = new ArrayList<>();
		List<SingleCourse> singleCourselist = new ArrayList<>();
		List<CourseBehave> courseBehavelist = new ArrayList<>();
		List<Course_Student> course_Studentlist = new ArrayList<>();
		JSONArray jsonArray = new JSONArray();
		//创建需要储存的list

		/*获取每一个jsonarray并且转化为list进行储存*/
		if(!getStudentArray().isEmpty())
		{
			jsonArray = JSONArray.fromObject(getStudentArray());
			studentlist = jsonArray.toList(jsonArray, new Student(), new JsonConfig());
			jsonArray.clear();
		}
		if(!getCourseArray().isEmpty())
		{
			jsonArray = JSONArray.fromObject(getCourseArray());
			courselist = jsonArray.toList(jsonArray, new Course(), new JsonConfig());
			jsonArray.clear();
		}
		if(!getSingleCourseArray().isEmpty())
		{
			jsonArray = JSONArray.fromObject(getSingleCourseArray());
			singleCourselist = jsonArray.toList(jsonArray, new SingleCourse(), new JsonConfig());
			jsonArray.clear();
		}
		if(!getCourseBehaveArray().isEmpty())
		{
			jsonArray = JSONArray.fromObject(getCourseBehaveArray());
			courseBehavelist = jsonArray.toList(jsonArray, new CourseBehave(), new JsonConfig());
			jsonArray.clear();
		}
		if(!getCourse_StudentArray().isEmpty())
		{
			jsonArray = JSONArray.fromObject(getCourse_StudentArray());
			course_Studentlist = jsonArray.toList(jsonArray, new Course_Student(), new JsonConfig());
		}

		try{
			if(syncService.backupData(getTid(), studentlist, courselist
					, singleCourselist, courseBehavelist, course_Studentlist) == 1)
			{
				map.put("result", "success");
				this.setSyncJson(map);
				syncService.updateSyncTime(getTid(), new Date());			//更新同步信息
				return "backupfinish";
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		map.put("result", "fail");
		this.setSyncJson(map);
		return "backupfinish";
	}

}
